const mysql = require("mysql")

class DBConnection{

    connPool;

    constructor () {
        this.connPool = mysql.createPool({
            "host": "127.0.0.1",
            "port": "3306",
            "database": "test",
            "user": "root",
            "password": "root",
            "connectionLimit": 20,
            "multipleStatements": true,
        })
    }

    query(sql, params = []) {
        return new Promise((resolve, reject) => {
            this.connPool.getConnection((err,conn) => {
            if (err) {
                console.log(`数据库连接失败:${err.message}`);
            }else{
                conn.query(sql, params, (err, res) => {
                if (err) {
                    console.log('数据库查询失败', err)
                    reject(err)
                } else {
                    resolve(res)
                }
                })
                conn.release();//释放连接
            }
            })
        })
    }
    

}

module.exports = DBConnection;